package com.dsl.dslallinone.service;

import com.dsl.dslallinone.common.config.*;
import com.dsl.dslallinone.dto.DslQuery;
import com.dsl.dslallinone.dto.Granularity;
import com.dsl.dslallinone.dto.TimeWindow;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.Collections;

/**
 * DSL示例服务 - 提供3个端到端示例
 */
@Service
public class DslExampleService {

    /**
     * 示例1：当月用水Top5楼栋占比（pie）
     */
    public DslQuery getWaterTop5BuildingExample() {
        DslQuery query = new DslQuery();
        
        // 时间配置：当月
        TimeWindow window = new TimeWindow(LocalDate.of(2025, 8, 1), LocalDate.of(2025, 8, 31));
        query.setTime(new TimeConfig(window, Granularity.DAY));
        
        // 维度
        query.setDimensions(Arrays.asList("building_id"));
        
        // 指标：求和 + 占比
        MetricConfig sumMetric = new MetricConfig("sum", "value", "sum_value", "m3");
        MetricConfig pctMetric = new MetricConfig("pct_of_total", "sum(value)", "pct_of_total", "%");
        query.setMetrics(Arrays.asList(sumMetric, pctMetric));
        
        // 过滤条件
        FilterConfig energyFilter = new FilterConfig("energy_type", "eq", "water");
        query.setFilter(Collections.singletonList(energyFilter));
        
        // 分组和排序
        query.setGroupBy(Collections.singletonList("building_id"));
        OrderConfig order = new OrderConfig("sum_value", "desc");
        query.setOrderBy(Collections.singletonList(order));
        
        // 限制Top5
        query.setLimit(5);
        
        return query;
    }

    /**
     * 示例2：近30日用电总量（日粒度）+ yoy/mom（line）
     */
    public DslQuery getElectricYoyMomExample() {
        DslQuery query = new DslQuery();
        
        // 时间配置：近30日
        TimeWindow window = new TimeWindow(LocalDate.of(2025, 7, 1), LocalDate.of(2025, 7, 30));
        query.setTime(new TimeConfig(window, Granularity.DAY));
        
        // 维度
        query.setDimensions(Arrays.asList("building_id"));
        
        // 指标：总量 + 同比 + 环比
        MetricConfig sumMetric = new MetricConfig("sum", "value", "sum_value", "kWh");
        MetricConfig yoyMetric = new MetricConfig("yoy", "sum(value)", "sum_value_yoy", "%");
        MetricConfig momMetric = new MetricConfig("mom", "sum(value)", "sum_value_mom", "%");
        query.setMetrics(Arrays.asList(sumMetric, yoyMetric, momMetric));
        
        // 过滤条件
        FilterConfig energyFilter = new FilterConfig("energy_type", "eq", "electric");
        query.setFilter(Collections.singletonList(energyFilter));
        
        // 分组和排序
        query.setGroupBy(Collections.singletonList("building_id"));
        OrderConfig order = new OrderConfig("time_bucket", "asc");
        query.setOrderBy(Collections.singletonList(order));
        
        query.setLimit(1000);
        
        return query;
    }

    /**
     * 示例3：今日小时占用率（realtime，按楼层折线）
     */
    public DslQuery getRealtimeOccupancyExample() {
        DslQuery query = new DslQuery();
        
        // 时间配置：今日小时
        LocalDate today = LocalDate.now();
        TimeWindow window = new TimeWindow(today, today);
        query.setTime(new TimeConfig(window, Granularity.HOUR));
        
        // 维度
        query.setDimensions(Arrays.asList("floor_id"));
        
        // 指标：占用率
        MetricConfig occupiedMetric = new MetricConfig("avg", "occupied", "avg_occupied", "%");
        query.setMetrics(Collections.singletonList(occupiedMetric));
        
        // 过滤条件：指定楼层
        FilterConfig floorFilter = new FilterConfig("floor_id", "in", Arrays.asList("101", "102"));
        query.setFilter(Collections.singletonList(floorFilter));
        
        // 分组和排序
        query.setGroupBy(Collections.singletonList("floor_id"));
        OrderConfig order = new OrderConfig("time_bucket", "asc");
        query.setOrderBy(Collections.singletonList(order));
        
        // 后处理：缺失值补零
        query.setPost(new PostConfig("zero"));
        
        // 指定实时数据源
        query.setSource("realtime");
        query.setLimit(100);
        
        return query;
    }
}
